package icu.qx.gbs.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.gbs.domain.GbsBomMaterial;
import icu.qx.gbs.domain.GbsGood;
import icu.qx.gbs.domain.param.GbsBomParam;
import icu.qx.gbs.service.IGbsBomMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.gbs.domain.GbsBom;
import icu.qx.gbs.service.IGbsBomService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * BOM清单Controller
 *
 * @author yong
 * @date 2024-06-12
 */
@RestController
@RequestMapping("/gbs/bom")
public class GbsBomController extends BaseController {
    @Autowired
    private IGbsBomService gbsBomService;

    @Autowired
    private CodeUtils codeUtils;

    @Autowired
    private IGbsBomMaterialService gbsBomMaterialService;

    /**
     * 查询BOM清单列表
     */
    @RequiresPermissions("gbs:bom:list")
    @GetMapping("/list")
    public TableDataInfo list(GbsBom gbsBom) {
        startPage();
        List<GbsBom> list = gbsBomService.selectGbsBomList(gbsBom);
        return getDataTable(list);
    }

    /**
     * 查询BOM清单列表
     */
    @RequiresPermissions("gbs:bom:list")
    @GetMapping("/all")
    public AjaxResult all(GbsBom gbsBom) {
        List<GbsBom> list = gbsBomService.selectGbsBomList(gbsBom);
        return AjaxResult.success(list);
    }

    /**
     * 导出BOM清单列表
     */
    @RequiresPermissions("gbs:bom:export")
    @Log(title = "BOM清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GbsBom gbsBom) {
        List<GbsBom> list = gbsBomService.selectGbsBomList(gbsBom);
        ExcelUtil<GbsBom> util = new ExcelUtil<GbsBom>(GbsBom.class);
        util.exportExcel(response, list, "BOM清单数据");
    }

    /**
     * 获取BOM清单详细信息
     */
    @RequiresPermissions("gbs:bom:query")
    @GetMapping(value = "/query")
    public AjaxResult getInfo(GbsBom gbsBom) {
        AjaxResult ajaxResult = success();

        // 根据主键获取 bom 详细信息
        gbsBom = gbsBomService.selectGbsBomByBomCode(gbsBom);

        GbsBomMaterial gbsBomMaterial = new GbsBomMaterial();
        gbsBomMaterial.setBmBomCode(gbsBom.getBomCode());
        gbsBomMaterial.setBmBomVersion(gbsBom.getBomVersion());
        List<GbsBomMaterial> selectedMaterials = gbsBomMaterialService.selectGbsBomMaterialList(gbsBomMaterial);

        ajaxResult.put("data", gbsBom);
        ajaxResult.put("selectedMaterials", selectedMaterials);

        return ajaxResult;
    }

    /**
     * 获取BOM清单详细信息
     */
    @RequiresPermissions("gbs:bom:query")
    @GetMapping(value = "/pdCode/{pdCode}")
    public AjaxResult getInfoByPdCode(@PathVariable("pdCode") String pdCode) {
        return success(gbsBomService.selectGbsBomByPdCode(pdCode));
    }

    /**
     * 新增BOM清单
     */
    @RequiresPermissions("gbs:bom:add")
    @Log(title = "BOM清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GbsBomParam gbsBomParam) {
        GbsBom gbsBom = gbsBomParam.getGbsBom();
        gbsBom.setUpdateBy(null);
        gbsBom.setUpdateTime(null);
        if (gbsBom.getBomCode() == null || gbsBom.getBomCode().isEmpty()) {
            String code = codeUtils.generateCode(TableNameEnum.GBS_BOM.getTableName());
            gbsBom.setBomCode(code);
        }
        int i = gbsBomService.insertGbsBom(gbsBom, gbsBomParam.getGbsBomMaterials(), gbsBomParam.getPbsProcedureMaterials());
        return toAjax(i);
    }

    /**
     * 修改BOM清单
     */
    @RequiresPermissions("gbs:bom:edit")
    @Log(title = "BOM清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GbsBomParam gbsBomParam) {
        GbsBom gbsBom = gbsBomParam.getGbsBom();
        int i = gbsBomService.updateGbsBom(gbsBom, gbsBomParam.getGbsBomMaterials(), gbsBomParam.getPbsProcedureMaterials());
        return toAjax(i);
    }

    /**
     * 删除BOM清单
     */
    @RequiresPermissions("gbs:bom:remove")
    @Log(title = "BOM清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bomCodes}")
    public AjaxResult remove(@PathVariable String[] bomCodes) {
        return toAjax(gbsBomService.deleteGbsBomByBomCodes(bomCodes));
    }
}
